package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.UserDaoImpl;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.Constants;
import com.qf.utils.MD5Utils;
import com.qf.utils.RandomUtils;

/**
 * @author Wy
 * @version V1.0
 * @Project javaWeb项目
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/5/23 10:59
 */
public class UserServiceImpl  implements UserService {

     public UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserServiceImpl() {
    }


    @Override
    public User logins(String userName, String password) {
        User byUserName = userDao.findByUserName(userName);
//2.判断用户是否为空
        if(byUserName==null){
            return  null;
        }

        if(!Constants.USER_ACTIVE.equals(byUserName.getStatus())){
            return  null;
        }

        //4.判断是不是消费者
        if(Constants.ROLE_ADMIN==byUserName.getRole()){
            return null;
        }
        String md5PassWord = MD5Utils.md5(password);
        if(md5PassWord.equals(byUserName.getPassword())){
            return  byUserName;
        }
        return null;
    }

    @Override
    public boolean checkduUser(String name) {
        User byUserName = userDao.findByUserName(name);
        if(byUserName==null){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public int createUser(User user) {
        //1. 密码使用md5进行加密
        String md5 = MD5Utils.md5(user.getPassword());
       user.setPassword(md5);
        //2. 设置消费者状态默认为激活状态
        user.setStatus(Constants.USER_ACTIVE);
        //3. 初始化用户角色为消费者
        user.setRole(Constants.ROLE_CUSTOMER);
        //4. 初始化, 消费者随机激活码
        user.setCode(RandomUtils.createActive());
        //5. 调用dao完成添加功能
        int i = userDao.addUser(user);

        return i;
    }
}
